/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.wikimark;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;

import org.h2.jdbcx.JdbcDataSource;

import com.caucho.quercus.servlet.QuercusServlet;

/**
 * The servlet creation factory
 * @author Sergey I. Salishev
 */
public final class ScriptServletProvider {
	
	private static final String PYTHON_SERVLET_CLASS = 
		"org.apache.cgiservlet.PythonServlet";
	
	private static final String RUBY_SERVLET_CLASS = 
		"org.apache.cgiservlet.RubyServlet";
	
	private static final String PHP_SERVLET_CLASS = 
		"com.caucho.quercus.servlet.QuercusServlet";	

	private static final Map<String, String> types = 
		new HashMap<String, String>();

	static {
		types.put("php", PHP_SERVLET_CLASS);
		types.put("php4", PHP_SERVLET_CLASS);
		types.put("php5", PHP_SERVLET_CLASS);
		types.put("py", PYTHON_SERVLET_CLASS);
		types.put("rb", RUBY_SERVLET_CLASS);
	}

	private final Class<? extends Servlet> servletClass; 	

	/** 
	 * @param servletClass the overriding servlet class to instantiate
	 */
	public ScriptServletProvider(Class<? extends Servlet> servletClass) {
		this.servletClass = servletClass;		
	}
	
	/**
	 * Creates the servlet for the fiven script file
	 * @param file script file
	 * @return new servlet
	 */
	public Servlet createServlet(File file) {
		return createServlet(getFileType(file));
	}
	
	private Servlet createServlet(String ext)  {
		try {
			Class<?> factory = servletClass != null ? servletClass
					: Class.forName(types.get(ext));

			Servlet res = (Servlet) factory.newInstance();
			//FIXME: refactor 
			if (ext.equals("php")) {
				initQuercus(res);
			}
			return res; 
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private void initQuercus(Servlet res) {
		QuercusServlet quercus = (QuercusServlet) res;		
			
		JdbcDataSource source = new JdbcDataSource();
	
		//FIXME: should be indirect
		source.setURL("jdbc:h2:build/playground/mediawiki/mediawiki;MODE=MYSQL");
		
		quercus.setDatabase(source);
	}

	private static String getFileType(File file) {
		String fileName = file.getName();
		int dotIndex = fileName.lastIndexOf('.');

		String ext = dotIndex >= 0 ? fileName.substring(dotIndex + 1, fileName
				.length()) : "";

		return ext;
	}

}
